﻿using Dexter.Repository.NHibernate.Configuration;
using Dexter.Repository.NHibernate.Helpers;

namespace Dexter.Repository.NHibernate.Repositories {
	public class GlobalRepository : IGlobalRepository {
		readonly INHibernateHelper nhelper;
		readonly INHibernateConfiguration nHibernateConfiguration;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:System.Object"/> class.
		/// </summary>
		public GlobalRepository ( INHibernateHelper nhelper , INHibernateConfiguration nHibernateConfiguration ) {
			this.nhelper = nhelper;
			this.nHibernateConfiguration = nHibernateConfiguration;
		}

		/// <summary>
		/// Gets a value indicating whether the repository ready.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is repository ready; otherwise, <c>false</c>.
		/// </value>
		public bool IsRepositoryReady {
			get { return nhelper.ShouldUpdateSchema ( ); }
		}

		public void SaveConfiguration ( string connectionString , string databaseType , string databaseSchema , bool enableCache , string databaseTablePrefix ) {
			nHibernateConfiguration.SaveConfiguration ( connectionString, databaseType, databaseSchema, databaseTablePrefix );
		}

		/// <summary>
		/// Inizializes the repository schema.
		/// </summary>
		public void InizializeRepositorySchema ( ) {
			nhelper.Configure (  );
			nhelper.InitializeSchema (  );
		}
	}
}
